﻿using Aurigma.GraphicsMill;
using Aurigma.GraphicsMill.Codecs;
using Aurigma.GraphicsMill.Transforms;
using Com.Hikvision.Wrapper.Core;
using Com.Hikvision.Wrapper.Core.Domain.EntranceControl;
using Com.Hikvision.Wrapper.Service.EntranceControl.RemoteEntranceControl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test.Hik
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string message;

            CardManage srvCardManage = new CardManage();
            DateTime now = DateTime.Now;

            UserInfo userInfo = new UserInfo();
            userInfo.CardNo = "90001";
            userInfo.Name = "yufc";

            userInfo.CardType = CHCNetSDK.CARD_TYPE.NORMAL;
            userInfo.CardPassword = "12345678";
            //userInfo.BeginTime = new DateTime(now.Year, now.Month, now.Day);
            //userInfo.EndTime = userInfo.BeginTime.AddYears(30);
            userInfo.EmployeeNo = 90001;
            userInfo.CardValid = 1;

            userInfo.DoorRight.Initialize();
            userInfo.DoorRight[0] = 1;
            //userInfo.DoorCount = 1;

            bool result = srvCardManage.CardInsert(out message, "192.168.2.199", 8000, userInfo, "admin", "qwer1234");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = "d:\\";//should be attention to the path is "d:\\" not "d:\"
            openFileDialog.Filter = "Face picture|*.jpg|All documents|*.*";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog.FileName;
            }
            if (!File.Exists(textBox1.Text))
            {
                MessageBox.Show("The face picture does not exist!");
                return;
            }
            FileStream fs = new FileStream(textBox1.Text, FileMode.OpenOrCreate);
            if (0 == fs.Length)
            {
                MessageBox.Show("The face picture is 0k,please input another picture!");
            }
            if (200 * 1024 < fs.Length)
            {
                MessageBox.Show("The face picture is larger than 200k,please input another picture!");
            }
            fs.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream(textBox1.Text, FileMode.OpenOrCreate);
            if (0 == fs.Length)
            {
                MessageBox.Show("The face picture is 0k,please input another picture!");
                return;
            }
            if (200 * 1024 < fs.Length)
            {
                MessageBox.Show("The face picture is larger than 200k,please input another picture!");
                return;
            }


            int iLen = (int)fs.Length;
            byte[] by = new byte[iLen];

            fs.Read(by, 0, iLen);
            fs.Close();



            string message;

            FaceManage srvCardManage = new FaceManage();
            DateTime now = DateTime.Now;

            UserInfo userInfo = new UserInfo();
            userInfo.CardNo = "90001";
            userInfo.Name = "yufc";

            userInfo.CardType = CHCNetSDK.CARD_TYPE.NORMAL;
            userInfo.CardPassword = "12345678";
            userInfo.BeginOfValidity = new DateTime(now.Year, now.Month, now.Day);
            userInfo.EndOfValidity = new DateTime(2099, 12, 31) ;
            userInfo.EmployeeNo = 90001;
            userInfo.CardValid = 1;

            userInfo.DoorRight.Initialize();
            userInfo.DoorRight[0] = 1;
            //userInfo.DoorCount = 1;

            userInfo.Avator = by;


            CardManage cardManage = new CardManage();
            bool result = cardManage.CardInsert(out message, "192.168.2.199", 8000, userInfo, "admin", "qwer1234");

            FaceManage faceManage = new FaceManage();

            result = srvCardManage.FaceInsert(out message, "192.168.2.199", 8000, userInfo, "admin", "qwer1234");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            using (var reader = ImageReader.Create(textBox1.Text))
            using (var resize = new Resize(480, 640, ResizeInterpolationMode.High))
            {
                //using (var unsharpMask = new UnsharpMask(0.35f, 1, 0))
                using (var writer = ImageWriter.Create(FileFormat.Jpeg, $@"C:\Users\yufc\Desktop\A111\新建文件夹\{ Guid.NewGuid().ToString().ToUpper()}.jpg"))
                {
                    //Pipeline.Run(reader + resize + unsharpMask + writer);
                    Pipeline.Run(reader + resize + writer);
                }
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
